#!/usr/bin/env python
#-*- encoding: utf-8 -*-
import xlrd
import csv
import xlwt
import sys
import os
#reload(sys)
#sys.setdefaultencoding('utf-8')

def csv2xls(src,dst):
	newfilename = ""
	if dst == "":
		newfilestr=src[:len(src)-3]
		newfilename=newfilestr+'xls'
	else:
		newfilename = dst
	print(newfilename)
	newfile=xlwt.Workbook(encoding='gbk')
	table=newfile.add_sheet('Sheet1',cell_overwrite_ok=True)
	#txt='asdf1#'
	#txt.decode('utf8')
	#table.write(0,0,txt.decode('utf8'))
#	data=file(src,'rb')
	with open(src, newline='') as csvfile:
		reader=csv.reader(csvfile)
		col=-1
		row=-1
		for line in reader:
			row+=1
			col=-1
			for column in line:
				col+=1
				table.write(row,col,column)
		newfile.save(newfilename)

if __name__=='__main__': 
	p = sys.argv
	l = len(p)
	if l<=1 or l>3:
		print("usage:./csv2xls [source file]\n     ./csv2xls [source file] [destination file]")
	elif l==2:
		csv2xls(p[1],"")
	elif l==3:
		csv2xls(p[1],p[2])
	else:
		print("usage:./csv2xls [source file]\n     ./csv2xls [source file] [destination file]")
